package cn.kgc.ssm.controller;

import cn.kgc.ssm.annotations.LogInfos;
import cn.kgc.ssm.dto.UserPageDTO;
import cn.kgc.ssm.entity.User;
import cn.kgc.ssm.enums.Operations;
import cn.kgc.ssm.response.ResponseResult;
import cn.kgc.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/11/14
 * @description
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

   // 登录
    @GetMapping("login")
    public ResponseResult  login(String username, String password, String code, HttpServletRequest request){

        return  userService.login(username,password,code,request);
    }

    // 个人信息获取
    @GetMapping("personalInfo")
    public ResponseResult  personalInfo(HttpSession session){

        return  userService.personalInfo(session);

    }


    // 个人信息编辑  editPersonalInfo
    @PostMapping("editPersonalInfo")
    public ResponseResult  editPersonalInfo(@RequestBody User user){

        return  userService.editPersonalInfo(user);

    }

    // 个人密码修改  editPersonalPwd
    @PostMapping("editPersonalPwd")
    public ResponseResult  editPersonalPwd(String oldPassword,String newPassword,HttpSession session){

        return  userService.editPersonalPwd(oldPassword,newPassword,session);

    }



    // 退出登录
    @GetMapping("logout")
    public ResponseResult  logout(HttpSession session){

        // 销毁会话
        session.invalidate();
        return  ResponseResult.success().msg("安全退出");
    }

    //  用户信息分页
    @GetMapping("page")
    public ResponseResult  userPage(UserPageDTO userPageDTO){

        return  userService.userPage(userPageDTO);

    }

    // 添加用户
    @LogInfos(model = "用户模块",operation = Operations.ADD)
    @PostMapping("add")
    public ResponseResult  addUser(User user){

        return  userService.addUser(user);

    }


    //编辑用户信息
    @LogInfos(model = "用户模块",operation = Operations.UPDATE)
    @PostMapping("edit")
    public ResponseResult  editUser(User user){

        return  userService.editUser(user);

    }


    // 删除用户信息
    @PostMapping("delete")
    @LogInfos(model = "用户模块",operation = Operations.DELETE)
    public ResponseResult  deleteUser(@RequestBody List<Integer> ids){

        return  userService.deleteUser(ids);
    }


    // 重置密码
    @PostMapping("resetPassword/{id}")
    public ResponseResult resetPassword(@PathVariable("id") Integer id){


       return   userService.resetPassword(id);
    }



}
